package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File artDir;
    static {
        artDir=new File("./articles");
        if(!artDir.exists()){
            artDir.mkdirs();
        }
    }
    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理用户发表文章");
        String topic=request.getParameter("topic");
        String writer=request.getParameter("writer");
        String article=request.getParameter("article");


        if(
                topic==null||topic.isEmpty()||writer==null||writer.isEmpty()
                ||article==null||article.isEmpty()
        ){
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        Article art=new Article(topic,writer,article);
        File file=new File(artDir,topic+".obj");
        if(file.exists()){
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {

            try (
                    FileOutputStream fos = new FileOutputStream(file);
                    ObjectOutputStream oos = new ObjectOutputStream(fos)
            ) {
                oos.writeObject(art);
                response.sendRedirect("/article_success.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理文章列表");

        List<Article> articleList=new ArrayList<>();
        File[] subs=artDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File sub:subs){
            try(
                    FileInputStream fis=new FileInputStream(sub);
                    ObjectInputStream ois=new ObjectInputStream(fis);
                    ){

                Article article=(Article)ois.readObject();
                articleList.add(article);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }


        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw=response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"1\">");

            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("</tr>");


            for(Article article:articleList) {
                pw.println("<tr>");
                pw.println("<td>"+article.getTopic()+"</td>");
                pw.println("<td>"+article.getWriter()+"</td>");
                pw.println("</tr>");
            }

            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
